define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'xiservice/withdraw/index' + location.search,
                    add_url: 'xiservice/withdraw/add',
                    edit_url: 'xiservice/withdraw/edit',
                    //del_url: 'xiservice/withdraw/del',
                    multi_url: 'xiservice/withdraw/multi',
                    import_url: 'xiservice/withdraw/import',
                    benfit_url:'xiservice/benfit/index',
                    table: 'xiservice_user_withdraw',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                search: false,     
                showExport: false,
                showToggle: false,
                showColumns: false,  
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'),operate:false},
                        {field: 'user.avatar', title: __('User.avatar'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'user.username', title: __('服务人员信息'), operate:false, formatter: function (val,row) {
                              var url = $.fn.bootstrapTable.defaults.extend.benfit_url+"?ids="+row.user.id;
                              var html='<a href="javascript:;" class="btn-dialog"  data-url="'+url+'">';
                              html += '<div style="text-align:left">服务人员姓名:'+row.user.username+'</div>';
                              html += '<div style="text-align:left">手机号码:'+row.user.mobile+'</div>';
                              html += '<div style="text-align:left">冻结金额:'+row.user.freeze_money+'</div>';
                              html += '<div style="text-align:left">总收益:'+row.user.total_money+'</div>';
                               html += '</a>';
                             return html;
                        }},                        
                        {field: 'name', title: __('Name'), operate: 'LIKE'},
                        {field: 'bankname', title: __('Bankname'), operate: false},
                        {field: 'bankcard', title: __('Bankcard'), operate: false},
                        {field: 'money', title: __('Money'), operate:false},
                        {field: 'checkinfo', title: __('Checkinfo'), searchList: {"0":__('Checkinfo 0'),"1":__('Checkinfo 1'),"2":__('Checkinfo 2')}, formatter: Table.api.formatter.normal},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate,                         
                          buttons: [
                                {
                                    name: 'addtabs',
                                    text: __('同意'),
                                    title: __('同意'),
                                    classname: 'btn btn-xs btn-warning btn-ajax',
                                    icon: 'fa fa-check-square',
                                    url: 'xiservice/withdraw/agree?ids={id}',
                                    refresh:true,
                                    visible:function(row){
                                        if(row['checkinfo']==0 && (row['adminrole']==6 || row['adminrole']==1) && row['auth_status']==0){
                                            return true;
                                        }
                                         return false;
                                    }
                                },
                                {
                                    name: 'addtabs',
                                    text: __('同意'),
                                    title: __('同意'),
                                    classname: 'btn btn-xs btn-warning btn-ajax',
                                    icon: 'fa fa-check-square',
                                    url: 'xiservice/withdraw/agree2?ids={id}',
                                    refresh:true,
                                    visible:function(row){
                                        if(row['checkinfo']==0 && (row['adminrole']==7 || row['adminrole']==1) && row['auth_status']==1){
                                            return true;
                                        }
                                         return false;
                                    }
                                },
                                {
                                    name: 'addtabs',
                                    text: __('同意'),
                                    title: __('同意'),
                                    classname: 'btn btn-xs btn-warning btn-ajax',
                                    icon: 'fa fa-check-square',
                                    url: 'xiservice/withdraw/agree3?ids={id}',
                                    refresh:true,
                                    visible:function(row){
                                        if(row['checkinfo']==0 && (row['adminrole']==8 || row['adminrole']==1) && row['auth_status']==2){
                                            return true;
                                        }
                                         return false;
                                    }
                                },
                                {
                                    name: 'addtabs',
                                    text: __('拒绝'),
                                    title: __('拒绝'),
                                    classname: 'btn btn-xs btn-warning  btn-dialog',
                                    icon: 'fa fa-close',
                                    url: 'xiservice/withdraw/refuse?ids={id}',
                                    visible:function(row){
                                         if(row['checkinfo']==0){
                                            if(row['adminrole']==6 && row['auth_status']!=0)
                                            {
                                                return false;
                                            }
                                            if(row['adminrole']==7 && row['auth_status']!=1)
                                                {
                                                    return false;
                                                }
                                                if(row['adminrole']==8 && row['auth_status']!=2)
                                                    {
                                                        return false;
                                                    }
                                             return true;
                                         }
                                         return false;
                                    }
                                },
                                {
                                    name: 'addtabs',
                                    text: __('详情'),
                                    title: __('详情'),
                                    classname: 'btn btn-xs btn-warning  btn-dialog',
                                    icon: 'fa fa-eye',
                                    url: 'xiservice/withdraw/edit?ids={id}',
                                    visible:function(row){
                                         if(row['checkinfo']>0){
                                             return true;
                                         }
                                         return false;
                                    }
                                }
                            ],
                            formatter: Table.api.formatter.buttons,
                     }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        agree: function () {
            Controller.api.bindevent();
        },
        refuse: function () {
            Controller.api.bindevent();
        },       
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
